﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmLeaveWorkInfo : DockContent
    {
        int _LWID;
        int _EmployeeID;
        LeaveClient wcfLeave;
        LeaveWorkInfo _LWInfo;
        DialogResult dlgResult;
        LeaveWorkClient wcfLW;
        PositionDetailClient _wcfPD;
        RoleRightInfo listRole;
        bool isChange = false;
        //int _TotalLeaveDay = 0;
        int _CountLeave = 0;

        public frmLeaveWorkInfo(int LWID, int EmployeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            Language();
            wcfLW = Service.LeaveWork();
            _wcfPD = Service.PositionDetail();
            wcfLeave = Service.Leave();
            dlgResult = DialogResult.Cancel;
            _LWInfo = new LeaveWorkInfo();
            LoadLeave();

            _LWID = LWID;
            _EmployeeID = EmployeeID;
            txtEmpName.Tag = 0;

            if (_LWID > 0)
            {
                _LWInfo = wcfLW.LeaveWork_GetInfo(_LWID);
                Info2Form();
            }
            listRole = _listRole;
            isChange = false;
        }

        public void Language()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString("thongtinngaynghi");
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }

        private void Info2Form()//Hiển thị thông tin ra form
        {
            GetEmployeeName(_LWInfo.EmployeeID);
            cboLeave.SelectedValue = _LWInfo.LeaveID;
            txtFromDate.Text = _LWInfo.FromDate.ToString("dd/MM/yyyy");
            txtToDate.Text = _LWInfo.ToDate.ToString("dd/MM/yyyy");
            txtReason.Text = _LWInfo.Reason;

            txtTotalDaysTaken.Text = _LWInfo.TotalDays.ToString("0.0");
            isChange = false;
        }

        private void Form2Info()//Chuyển dữ liệu từ form nhập vào Info
        {
            _LWInfo.ID = _LWID;
            _LWInfo.EmployeeID = Convert.ToInt32(txtEmpName.Tag);
            _LWInfo.LeaveID = Convert.ToInt32(cboLeave.SelectedValue);
            _LWInfo.FromDate = DateTime.ParseExact(txtFromDate.Text, "dd/MM/yyyy", null);
            _LWInfo.ToDate = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null);
            _LWInfo.Reason = txtReason.Text;
            _LWInfo.TotalDays = Convert.ToDecimal(txtTotalDaysTaken.Text);
        }


        private void LoadLeave()
        {
            cboLeave.DisplayMember = "Leave_Name";
            cboLeave.ValueMember = "Leave_ID";
            cboLeave.DataSource = wcfLeave.Leave_GetByBranchCode(Program.strBranchCode).Tables[0];
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            TimeSpan ts;
            DateTime DateVal;
            if (!DateTime.TryParseExact(txtFromDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateVal))
            {
                txtFromDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!DateTime.TryParseExact(txtToDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateVal))
            {
                txtToDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
            {
                ts = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null) - DateTime.ParseExact(txtFromDate.Text, "dd/MM/yyyy", null);

                if (ts.TotalDays < 0)
                {
                    txtToDate.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            _LWID = 0;
            txtFromDate.Text = "";
            txtToDate.Text = "";
            txtReason.Text = "";
            txtTotalDaysTaken.Text = "0";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            if (CheckValid())
            {
                Form2Info();
                if (_LWID > 0)//update
                {
                    if (!listRole.Update)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (wcfLW.LeaveWork_Update(_LWInfo))
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
                else//insert
                {
                    if (!listRole.Create)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    //if (_CountLeave + Convert.ToInt32(ts.TotalDays) <= _TotalLeaveDay)
                    //{
                    _LWInfo.ID = wcfLW.LeaveWork_Insert(_LWInfo);
                    if (_LWInfo.ID > 0)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                    //}
                    //else
                    //    MessageBox.Show("Cán bộ này đã nghỉ hết ngày qui định!\nChọn loại ngày nghỉ khác", UIParamater.TitleProject, MessageBoxButtons.OKCancel);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //this.Dispose();
            this.Close();
        }

        private void frmLeaveWorkInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void frmLeaveWorkInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }

        private void cboLeave_SelectedIndexChanged(object sender, EventArgs e)
        {
            //xử lý hiện ngày TotalDaysTaken va AfterUse
            //_TotalLeaveDay = wcfLeave.Leave_GetInfo(Convert.ToInt32(cboLeave.SelectedValue)).DateNumber;
            //if (wcfLW.LeaveWork_CountDay(Convert.ToInt32(cboEmployee.SelectedValue), Convert.ToInt32(cboLeave.SelectedValue)) > 0)
            //    _CountLeave = wcfLW.LeaveWork_CountDay(Convert.ToInt32(cboEmployee.SelectedValue), Convert.ToInt32(cboLeave.SelectedValue));
            //else
            //    _CountLeave = 0;
            //DataTable dt = new DataTable();
            //dt = wcfLW.LeaveWork_GetDayUsed(Convert.ToInt32(cboEmployee.SelectedValue), Convert.ToInt32(cboLeave.SelectedValue)).Tables[0];
            //if (dt.Rows.Count > 0)
            //{
            //    _DayUse = Convert.ToInt32(dt.Rows[0]["LW_AfterUse"]);
            //    txtBeforeUse.Text = _DayUse.ToString();
            //}
            //else
            //    txtBeforeUse.Text = (_TotalLeaveDay - _CountLeave).ToString();
            //txtAfterUse.Text = "0";
        }

        private void cboEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (_LoadLeave)
            //{
            //    //xử lý hiện ngày TotalDaysTaken va AfterUse
            //    _TotalLeaveDay = wcfLeave.Leave_GetInfo(Convert.ToInt32(cboLeave.SelectedValue)).DateNumber;
            //    if (wcfLW.LeaveWork_CountDay(Convert.ToInt32(cboEmployee.SelectedValue), Convert.ToInt32(cboLeave.SelectedValue)) > 0)
            //        _CountLeave = wcfLW.LeaveWork_CountDay(Convert.ToInt32(cboEmployee.SelectedValue), Convert.ToInt32(cboLeave.SelectedValue));
            //    else
            //        _CountLeave = 0;
            //    DataTable dt = new DataTable();
            //    dt = wcfLW.LeaveWork_GetDayUsed(Convert.ToInt32(cboEmployee.SelectedValue), Convert.ToInt32(cboLeave.SelectedValue)).Tables[0];
            //    if (dt.Rows.Count > 0)
            //    {
            //        _DayUse = Convert.ToInt32(dt.Rows[0]["LW_AfterUse"]);
            //        txtBeforeUse.Text = _DayUse.ToString();
            //    }
            //    else
            //        txtBeforeUse.Text = (_TotalLeaveDay - _CountLeave).ToString();
            //    txtAfterUse.Text = "0";
            //}
        }

        private void txtToDate_Leave(object sender, EventArgs e)
        {
            TimeSpan ts;
            decimal CountWK = 0;
            decimal CountHoliday = 0;
            decimal _TotalLeave = 0;
            DateTime _date1 = DateTime.MinValue;
            DateTime _date2 = DateTime.MinValue;
            DateTime DateVal;

            if (txtFromDate.Text != "/  /")
                if (!DateTime.TryParseExact(txtToDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateVal))
                {
                    txtToDate.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    _date1 = DateTime.ParseExact(txtFromDate.Text, "dd/MM/yyyy", null);
                    _date2 = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null);
                    CountWK = UISetting.CountWeekend(_date1, _date2);
                    CountHoliday = UISetting.CountHoliday(_date1, _date2);
                    ts = _date2 - _date1;
                    _TotalLeave = Convert.ToDecimal(ts.TotalDays);
                    if (_TotalLeave < 0)
                    {
                        txtToDate.Focus();
                        MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                        txtTotalDaysTaken.Text = (_TotalLeave - CountWK - CountHoliday + 1).ToString("#0.0");
                }
        }

        private void txtFromDate_Leave(object sender, EventArgs e)
        {
            DateTime DateVal;
            if (txtFromDate.Text != "/  /")
                if (!DateTime.TryParseExact(txtFromDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateVal))
                {
                    txtFromDate.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
        }

        private void btnSelectEmployee_Click(object sender, EventArgs e)
        {
            frmEmployeeList f = new frmEmployeeList(true);
            if (f.ShowDialog() == DialogResult.OK)
            {
                GetEmployeeName(f.EmpID);
            }
        }

        void GetEmployeeName(int employeeid)
        {
            EmployeeClient _wcfEmployee = Service.Employee();
            EmployeeInfo _EmployeeInfo = _wcfEmployee.Employee_GetInfo(employeeid);
            txtEmpName.Tag = _EmployeeInfo.ID;
            txtEmpName.Text = _EmployeeInfo.LastName + " " + _EmployeeInfo.FirstName;
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
